package com.xiaoyg.jvm;

import sun.misc.Launcher;

import java.net.URL;

/**
 * 沙河机制：
 * 1、避免API被篡改
 * 2、避免重复加载类
 *  Tomcat 使用的不是双亲委派机制：同一服务器部署不同版本的依赖
 *  打破双亲委派机制，自定义WebClassLoader
 *
 *
 *  对象创建过程：
 *  类加载检查，加载类，分配内存，设置0值，设置对象头，执行init方法
 */
public class ClassLoadUtils {

    public static void main(String[] args) {

        System.out.println(String.class.getClassLoader());

        System.out.println(com.sun.crypto.provider.DESKeyFactory.class.getClassLoader());

        System.out.println(ClassLoadUtils.class.getClassLoader());

        System.out.println();

        ClassLoader appClassLoader = ClassLoader.getSystemClassLoader();
        ClassLoader extClassLoader = appClassLoader.getParent();
        ClassLoader bootstrapLoader = extClassLoader.getParent();

        System.out.println("appClassLoader :"+ appClassLoader);
        System.out.println("extClassLoader :"+ extClassLoader);
        System.out.println("bootstrapLoader :"+ bootstrapLoader);

        System.out.println();
        System.out.println("bootstrapLoader加载以下文件：");
        URL[] urLs = Launcher.getBootstrapClassPath().getURLs();

        for (int i = 0; i < urLs.length; i++) {
            System.out.println(urLs[i]);
        }

        System.out.println();
        System.out.println("extClassLoader加载以下文件：");
        System.out.println(System.getProperty("java.ext.dirs"));

        System.out.println();
        System.out.println("appClassLoader加载以下文件：");
        System.out.println(System.getProperty("java.class.path"));



    }

}
